package com.learn.metrics.report.csv;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.codahale.metrics.CsvReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;

import java.io.File;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

/**
 * @author XuYang
 * @since 2025/2/19 11:22
 */
public class CsvReporterExample {

    public static void start(Integer interval) {
        MetricRegistry registry = new MetricRegistry();

        String directoryStr = Path.of(CsvReporterExample.class.getClassLoader().getResource(".").getPath().substring(1), "report/").toString();
        File targetDirectory = FileUtil.mkdir(directoryStr);
        // 打印统计信息
        CsvReporter.forRegistry(registry).build(targetDirectory).start(interval, TimeUnit.SECONDS);

        Meter meter = registry.meter("scv");
        int count = 0;
        while (count++ < 10) {
            meter.mark();
            ThreadUtil.sleep(RandomUtil.randomInt(500, 1000));
        }
    }
}
